package com.wljy.developtool.service.impl;

import com.wljy.developtool.config.cp.WxCpConfiguration;
import com.wljy.developtool.config.cp.WxCpProperties;
import com.wljy.developtool.service.INoticeService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class NoticeServiceImpl implements INoticeService {

    @Autowired
    private WxCpProperties properties;

    @Override
    public WxCpMessageSendResult sendError(String message) throws WxErrorException {
        message = "ERROR : " + message;
        return sendTextMessageToCp(message);
    }

    @Override
    public WxCpMessageSendResult sendWarn(String message) throws WxErrorException {
        message = "WARN : " + message;
        return sendTextMessageToCp(message);
    }

    @Override
    public WxCpMessageSendResult sendInfo(String message) throws WxErrorException {
        message = "INFO : " + message;
        return sendTextMessageToCp(message);
    }

    private WxCpMessageSendResult sendTextMessageToCp(String errorMsg) throws WxErrorException {
        final WxCpService wxService = WxCpConfiguration.getCpService(
                properties.getAppConfigs().get(0).getAgentId());
        WxCpMessage wxCpMessage = new WxCpMessage();
        wxCpMessage.setToUser("ZhouYi");
        wxCpMessage.setMsgType("text");
        wxCpMessage.setAgentId(properties.getAppConfigs().get(0).getAgentId());
        wxCpMessage.setContent(errorMsg);
        return wxService.getMessageService().send(wxCpMessage);
    }
}
